package com.ys.controller.web.business;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.config.MenuId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bo.vo.W;
import com.ys.entity.amazonorder.TbBaseinfoSppl;
import com.ys.entity.sys.YsCol;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.R;

@Controller
@RequestMapping("/web/business/tbBaseinfoSppl")
public class WebTbBaseinfoSpplController {
	@Autowired
	private CommonApiService<TbBaseinfoSppl> commonService;
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){

		try {
			return commonService.toAdd(model, new TbBaseinfoSppl(), MenuId.TB_BaseinfoSppl, null, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 修改
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){

		try {
			return commonService.toEdit(model, id, new TbBaseinfoSppl(), MenuId.TB_BaseinfoSppl, null, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	//导出
	@RequestMapping("/exportTbBaseinfoSppl")
	@ResponseBody
	public R exportTbBaseinfoSppl(@ModelAttribute TbBaseinfoSppl cond, String conditions) throws Exception {

		cond.setPage(0).setSize(999999);
		cond = setSeachSql(cond);
		cond.setOrderby("A.CreateTime DESC");//排序，新添加在前
		//获取数据
		List<Map<String, Object>> listData = cond.queryMap();
		List<YsCol> listField = new YsCol()
				.setMenuId(MenuId.TB_BaseinfoSppl)
				.where("A.Code != 'ope'")
				.queryList();
		listField.add(new YsCol().setName("制单时间").setCode("createTime"));

		ExportExcelUtils<TbBaseinfoSppl> util = new ExportExcelUtils<>(new TbBaseinfoSppl());

		String toFilePath = util.exportExcel(listData, listField, "商品品类维护");
		UploadService.UploadDto dto =commonService.uploadLocalToServer(toFilePath);
		return R.ok(dto.getUrl());
	}
	/**
	 * 设置查询的cond的sql设置
	 * @param cond
	 * @return
	 */
	public TbBaseinfoSppl setSeachSql(TbBaseinfoSppl cond){
		cond.addField("B.code code,B.name name");
		cond.addLeftJoin("TP_ProductActive B ON B.code = A.code ");

		return cond;
	}


}
